#include "sk_storage/util.hpp"

namespace sk_storage {

EasySnowflake::EasySnowflake() {
  last_timestamp_ = 0;
  cnt_on_one_step_ = 0;
}

uint64_t EasySnowflake::NewId() {
  using namespace std::chrono;
  mu.lock();
  uint64_t now_timestamp_ =
      duration_cast<milliseconds>(system_clock::now().time_since_epoch())
          .count();
  if (now_timestamp_ != last_timestamp_) {
    last_timestamp_ = now_timestamp_;
    cnt_on_one_step_ = 0;
  } else {
    cnt_on_one_step_++;
  }
  uint64_t id = now_timestamp_ << 12 | cnt_on_one_step_;
  mu.unlock();
  return id;
}

} // namespace sk_storage
